﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super_Wall_Breaker.Sources.TeamElements.PlayersControls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Super_Wall_Breaker.Sources.GameEngine;

namespace Super_Wall_Breaker.Sources.TeamElements.Players
{
    /// <summary>
    /// This class represents a real player.
    /// </summary>
    public class RealPlayer : Player
    {
        // Controls of the player, ie defines keyboard keys or gamepad buttons.
        private ControlsState _playerControl;
        private PlayerIndex _playerIndex;


        /// <summary>
        /// Gets or sets the player control.
        /// </summary>
        /// <value>
        /// The player control.
        /// </value>
        public ControlsState PlayerControl
        {
            get { return _playerControl; }
            set { _playerControl = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RealPlayer"/> class.
        /// </summary>
        /// <param name="teamIndex">Index of the team.</param>
        /// <param name="playerIndex">Index of the player.</param>
        /// <param name="controlsType">Type of the controls.</param>
        /// <param name="position">The position of the bat.</param>
        /// <param name="speed">The speed of the bat.</param>
        /// <param name="sourceRectangle">The source rectangle, used to select .</param>
        public RealPlayer(TeamIndex teamIndex, PlayerIndex playerIndex, ControlsType controlsType, Vector2 position, Vector2 speed, Rectangle? sourceRectangle) : base(teamIndex,position,speed,sourceRectangle)
        {
            this._playerIndex = playerIndex;

            if(controlsType.Equals(ControlsType.GAMEPAD))
                this._playerControl = new GamePadControlsState(this._playerIndex);
            else
                this._playerControl = new KeyboardControlsState(this._playerIndex);
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if(this._playerControl.GetState().Equals(Controls.RIGHT))
            {
                this._bat.Speed = new Vector2(this._initialBatSpeed.X, 0);
            }
            else if (this._playerControl.GetState().Equals(Controls.LEFT))
            {
                this._bat.Speed = new Vector2(-this._initialBatSpeed.X, 0);
            }
            else if (this._playerControl.GetState().Equals(Controls.NONE))
            {
                this._bat.Speed = new Vector2(0, 0);
            }

            this._bat.Update(gameTime);
        }
    }
}
